statement uses a subquery. Although this SQL statement is not complex, if there are too many layers of nesting, it will make the SQL statement very difficult to read and maintain. Therefore, you can use table variables to solve this problem. The SQL statement is as follows:
Declare @ t table (countryregioncode nvarchar (3 ))Insert into @ T (countryregioncode) (s
allowed.5. The following clauses cannot be used in CTE_query_definition:(1) COMPUTE or COMPUTE by(2) ORDER by (unless the TOP clause is specified)(3) into(4) OPTION clause with query hint(5) for XML(6) for BROWSE6. If the CTE is used in a statement that is part of a batch, then the statement before it must end with a semicolon, as shown in the following SQL:declare @s nvarchar (3)Set @s = ' c% '; --Must add a semicolonWithT_tree as(Select Countryregi
like 'C % ')
Select * from person. StateProvince where CountryRegionCodeIn (select * from @ t)
Although the preceding SQL statement is more complex than the first method, the subquery is placed in the table variable @ t. This will make the SQL statement easier to maintain, however, there will be another problem. The server space is the performance loss. Because
itself, or it can refer to a pre-defined CTE in the same with clause. Forward references are not allowed.5. The following clauses cannot be used in CTE_query_definition:(1) COMPUTE or COMPUTE by(2) ORDER by (unless the TOP clause is specified)(3) into(4) OPTION clause with query hint(5) for XML(6) for BROWSE6. If the CTE is used in a statement that is part of a batch, then the statement before it must end
about another problem, namely the performance loss. Because the table variables actually use temporary tables, which increases Additional I/O overhead, the table variables are not suitable for large data volumes and frequent queries. Therefore, another solution is provided in SQL Server 2005, which is the common table expression (CTE). Using
results of the query are omitted. Here we see that sometimes good table structure design is very important.Invite the month to complete the sharing on 2009.10.23 1:36.Some people are concerned about performance issues. No tests have been done at this time. A millions test report will be attached later. However, a two-point understanding invites the month to forget to add:First, the CTE is actually object-o
itself recursively or indirectly. For recursive common expressions, the implementation principle is the same, and you need to define two parts in the statement:
Basic statement
Recursive statements
The two parts of SQL are returned through the Union ALL connection result set: For example: In Adventurework, I want to know the level at which each employee is located, 0 is the highest Such complex queries have become so elegant
that sometimes good table structure design is very important.
Invite month to 2009.10.23 1:36 to complete the sharing.
Some people are concerned about performance issues. No tests have been done at this time. A millions test report will be attached later. However, there are two points of understanding invited month forgot to add:
One, the CTE is actually object-oriented, the basis of the operation is the C
c. C_Parent = s. PKID) as C_ParentName, C_Code as C_ParentCodeFrom CategorySelf c where C_Code like '2/%' The query contains all sub-categories with PKID 2 at a level not greater than 3.Copy codeThe Code is as follows:SELECT C_Name as C_Name, (Select top 1 C_Name from CategorySelf s where c. C_Parent = s. PKID) as C_ParentName, C_Code as C_ParentCodeFrom CategorySelf c where C_Code like '2/% 'and C_Level The query result is the same as the preceding one. We can see that, sometimes, a good tabl
SQL Server common table expressions (CTE) Implement recursion, cte Recursion
Introduction to common table expressions:
A common table expression (CTE) can be considered as a temporary result set defined within the execution range of a single SELECT, INSERT, UPDATE, DELETE, or create view statement.
CTE statement structure and CTE recursive query in SQL SERVER
CTE statement Structure
A common table expression (CTE) can be considered as a temporary result set defined within the execution range of a single SELECT, INSERT, UPDATE, DELETE, or create view statement.
CTE statement structure and CTE recursive query in SQL Server
CTE Statement Structure
A common table expression (CTE) can be considered a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A
T-SQL query: CTE, T-SQL: CTE
I have seen a simple example of the with as Clause in two books. I have not found any relevant information on the Internet.
Summary:
[Batch update of large tables]
[Batch Delete large tables]
[Keep only one row for completely repeated rows]
-- Create a test TABLE -- drop table [tabName] S
Directory
Background
Problem
Ideas
CTE
CTE recursive query
Conclusion
References
Background
I haven't written a blog for a long time. Recently I encountered a problem: "How can I traverse all the sub-Menus under a parent menu ?" Small and then use the CTE recursive query to solve this problem, sort the records for sharing.
Problem
How to trave
This article from: http://www.cnblogs.com/smailxiaobai/archive/2012/01/16/2323291.htmlSpecifies a temporary named result set, which is called a common table expression (CTE). The expression originates from a simple query and is defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. This clause can also be used in the CREATE VIEW statement as part of the statement's SELECT definition statement. A common table expres
person.CountryRegion where Name like ‘C%‘)
The query statement above uses a subquery. Although this SQL statement is not complex, it can make SQL statements very difficult to read and maintain if there are too many nested hierarchies. Therefore, you can also use table variables to solve this problem, the SQL statement is as follows:
About recursive queries that use a CTE (common table expression)----SQL Server 2005 and above
A common table expression (CTE) has an important advantage of being able to reference itself to create a recursive CTE. A recursive CTE is a common table expression that repeats an
The CTE (Common Table Expressions) is a version of SQL Server 2005. The specified temporary named result set, which is called a CTE. Similar to derived tables, is not stored as an object and is valid only for the duration of the query. Unlike a derived table, a CTE can be referenced and can be referenced multiple times
Recursive queries using CTE (common table expressions) ---- SQL Server 2005 and later versions
A common table expression (CTE) has an important advantage: It can reference itself to create a recursive CTE. Recursive CTE is a public table expression that repeats the initial
Label:Common table Expressions (CTE)When writing T-SQL code, it is often necessary to temporarily store some result sets. We have extensively used and introduced two methods for temporarily storing result sets: temporary tables and table variables. In addition to this, you can use the methods of common table expressions. The common table expression (Common table expression) is an attribute introduced by the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.